<template>
  <div>
    <!-- 顶部新增按钮 -->
    <div class="addBth">
      <el-button type="primary" @click="toAddNews">新增</el-button>
    </div>
    <!-- 表格内容展示 -->
    <div class="tableConent">
      <el-table :data="newsData" style="width: 100%">
        <!-- 序号 -->
        <el-table-column type="index" label="序号" width="180"> </el-table-column>
        <!-- 分类名称 -->
        <el-table-column
          prop="name"
          label="分类名称"
          width="200"
          align="center"
        >
        </el-table-column>
        <!-- 分类简介 -->
        <el-table-column prop="description" label="分类简介" align="center">
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="toEditNews(scope.row)" type="text" size="small"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog
      v-if="dialogVisible"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <!-- 表单区域 -->
      <el-form
        :model="newsForm"
        :rules="newsRules"
        ref="newsForm"
        label-width="100px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="newsForm.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="description">
          <el-input
            type="textarea"
            :rows="2"
            v-model="newsForm.description"
          ></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="no">
          <el-input v-model="newsForm.no"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmitNews"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      // allCategoryData:[],
      page: 1,
      // 数据数组
      newsData: [],
      // 控制模态框的显示与隐藏 默认隐藏
      dialogVisible: false,
      // 设置模态框标题
      dialogTitle: "",
      // 表单对象
      newsForm: {},
      // 表单验证规则
      newsRules: {
        name: [{ require: true,trigger: "blur" }],
      },
      // 所有资讯分类的数据
      allCategoryData: [],
    };
  },
  created() {
    this.getNewsData();
    // 自动获取所有资讯分类数据
    this.getAllCategoryData()
  },
  methods: {
    // 确定或修改资讯
    toSubmitNews() {
      // 验证
      console.log(this.newsForm)
      this.$refs["newsForm"].validate(async (valid) => {
        if (valid) {
          let res = await post("/category/saveOrUpdate", this.newsForm);
          if (res.status == 200) {
            // 成功提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            // 关闭模态框
            this.dialogVisible = false
            // 刷新页面
            this.getNewsData()
          } else {
            // 失败提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "error",
            });
          }
        } else {
          return false;
        }
      });
    },
    async getNewsData() {
      // 设置参数
      let params = {
        page: this.page,
      };
      let res = await get("/category/findAll", params);
      //  console.log(res);
      this.newsData = res.data;
    },
    // 新增
    toAddNews() {
      this.dialogTitle = "录入分类信息";
      this.newsForm = {};
      // 弹出模态框
      this.dialogVisible = true;
    },
    // 修改
    toEditNews(row) {
      this.dialogTitle = "录入分类信息";
      this.dialogVisible = true;
      this.newsForm = { ...row };
    },

    // 删除数据
    toDelete(id) {
      // console.log(id);
      // 消息提示确认
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确认按钮的执行方法
        .then(async () => {
          let res = await get("/category/deleteById", { id });
          if (res.status == 200) {
            // 请求成功给用户一个提示
            this.$message({
              type: "success",
              message: res.message,
            });
          } else {
            this.$message({
              type: "error",
              message: "删除失败!",
            });
          }
          //  刷新页面数据
          this.getNewsData();
        })
        // 点击取消按钮的执行方法
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取所有资讯分类
    async getAllCategoryData(){
      let res = await get('/category/findAll')
      // console.log(res.data);
      this.allCategoryData = res.data
    }
  },
};
</script>

<style scoped>
</style>
